package com.wz.dhs.config;

import com.wz.dhs.model.UserContextHolder;
import com.wz.dhs.service.base.impl.SystemServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author xyb
 * @Description 注解拦截器
 * @since 17:23 2022/7/14
 */
@Component("ss")
@RequiredArgsConstructor
public class UserPermissionConfig {

    private final SystemServiceImpl systemService;

    public boolean hasPermi(String permission) {
        Long userId = UserContextHolder.getUserInfo().getUserId();
        List<String> permissions = systemService.getMenuPermission(userId);
        String[] data = permission.split(",");
        if(data.length == 1){
            //后续需要角色的话可以查找角色存入 在给用户赋予角色时角色需要以：ROLE_ 开头，后面添加角色名
            return permissions.contains(permission);
        }
        else {
            //只需要其中满足一个权限码即可
            return Arrays.asList(data).stream().filter(var -> permissions.contains(var))
                    .count() > 0;
        }
    }

}
